{
 "metadata": {
  "name": ""
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import pandas as pd\n",
      "import numpy as np\n",
      "import json as js\n",
      "import urllib2 as ulib\n",
      "import simplejson\n",
      "import matplotlib.pyplot as plt"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# try reading with pandas... error??\n",
      "fdaquery=pd.read_json(\"http://api.fda.gov/drug/event.json?search=patient.drug.openfda.pharm_class_epc:\\\"nonsteroidal+anti-inflammatory+drug\\\"&count=patient.reaction.reactionmeddrapt.exact\")"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "fdaquery_request=ulib.Request(\"http://api.fda.gov/drug/event.json?search=patient.drug.openfda.pharm_class_epc:\\\"nonsteroidal+anti-inflammatory+drug\\\"&count=patient.reaction.reactionmeddrapt.exact\")"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "opener = ulib.build_opener()\n",
      "f = opener.open(fdaquery_request)\n",
      "fda_data=simplejson.load(f)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# get the list of adverse events\n",
      "fda_list=fda_data['results']"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 88
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "fda_list"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 89,
       "text": [
        "[{'count': 15586, 'term': 'NAUSEA'},\n",
        " {'count': 13792, 'term': 'DYSPNOEA'},\n",
        " {'count': 12829, 'term': 'MYOCARDIAL INFARCTION'},\n",
        " {'count': 12772, 'term': 'PAIN'},\n",
        " {'count': 12592, 'term': 'DIZZINESS'},\n",
        " {'count': 12588, 'term': 'DRUG INEFFECTIVE'},\n",
        " {'count': 11128, 'term': 'HEADACHE'},\n",
        " {'count': 11076, 'term': 'FATIGUE'},\n",
        " {'count': 10565, 'term': 'FLUSHING'},\n",
        " {'count': 10184, 'term': 'VOMITING'},\n",
        " {'count': 10044, 'term': 'DIARRHOEA'},\n",
        " {'count': 9271, 'term': 'ASTHENIA'},\n",
        " {'count': 8763, 'term': 'CEREBROVASCULAR ACCIDENT'},\n",
        " {'count': 8650, 'term': 'CHEST PAIN'},\n",
        " {'count': 8566, 'term': 'PRURITUS'},\n",
        " {'count': 8155, 'term': 'ARTHRALGIA'},\n",
        " {'count': 7779, 'term': 'FALL'},\n",
        " {'count': 7596, 'term': 'PAIN IN EXTREMITY'},\n",
        " {'count': 7342, 'term': 'OEDEMA PERIPHERAL'},\n",
        " {'count': 7174, 'term': 'ANXIETY'},\n",
        " {'count': 7131, 'term': 'PYREXIA'},\n",
        " {'count': 6858, 'term': 'HYPERTENSION'},\n",
        " {'count': 6489, 'term': 'DEPRESSION'},\n",
        " {'count': 6486, 'term': 'RENAL FAILURE ACUTE'},\n",
        " {'count': 6462, 'term': 'MALAISE'},\n",
        " {'count': 6436, 'term': 'RASH'},\n",
        " {'count': 6400, 'term': 'ANAEMIA'},\n",
        " {'count': 6315, 'term': 'BACK PAIN'},\n",
        " {'count': 5999, 'term': 'PNEUMONIA'},\n",
        " {'count': 5997, 'term': 'HYPOTENSION'},\n",
        " {'count': 5731, 'term': 'WEIGHT DECREASED'},\n",
        " {'count': 5667, 'term': 'ABDOMINAL PAIN'},\n",
        " {'count': 5662, 'term': 'INSOMNIA'},\n",
        " {'count': 5472, 'term': 'PARAESTHESIA'},\n",
        " {'count': 5146, 'term': 'BLOOD GLUCOSE INCREASED'},\n",
        " {'count': 5114, 'term': 'ABDOMINAL PAIN UPPER'},\n",
        " {'count': 4818, 'term': 'DRUG INTERACTION'},\n",
        " {'count': 4767, 'term': 'MYALGIA'},\n",
        " {'count': 4669, 'term': 'COUGH'},\n",
        " {'count': 4607, 'term': 'RENAL FAILURE'},\n",
        " {'count': 4606, 'term': 'ERYTHEMA'},\n",
        " {'count': 4482, 'term': 'GASTROINTESTINAL HAEMORRHAGE'},\n",
        " {'count': 4463, 'term': 'FEELING ABNORMAL'},\n",
        " {'count': 4441, 'term': 'CARDIAC FAILURE CONGESTIVE'},\n",
        " {'count': 4409, 'term': 'CONSTIPATION'},\n",
        " {'count': 4402, 'term': 'DEATH'},\n",
        " {'count': 4392, 'term': 'DEHYDRATION'},\n",
        " {'count': 4343, 'term': 'CONFUSIONAL STATE'},\n",
        " {'count': 4296, 'term': 'SOMNOLENCE'},\n",
        " {'count': 4276, 'term': 'HAEMOGLOBIN DECREASED'},\n",
        " {'count': 4270, 'term': 'PULMONARY EMBOLISM'},\n",
        " {'count': 4081, 'term': 'ATRIAL FIBRILLATION'},\n",
        " {'count': 3978, 'term': 'CONDITION AGGRAVATED'},\n",
        " {'count': 3963, 'term': 'BLOOD PRESSURE INCREASED'},\n",
        " {'count': 3961, 'term': 'HYPERHIDROSIS'},\n",
        " {'count': 3936, 'term': 'LOSS OF CONSCIOUSNESS'},\n",
        " {'count': 3902, 'term': 'WEIGHT INCREASED'},\n",
        " {'count': 3862, 'term': 'HYPOAESTHESIA'},\n",
        " {'count': 3855, 'term': 'MUSCLE SPASMS'},\n",
        " {'count': 3682, 'term': 'DEEP VEIN THROMBOSIS'},\n",
        " {'count': 3655, 'term': 'GAIT DISTURBANCE'},\n",
        " {'count': 3629, 'term': 'FEELING HOT'},\n",
        " {'count': 3620, 'term': 'INJURY'},\n",
        " {'count': 3603, 'term': 'DECREASED APPETITE'},\n",
        " {'count': 3598, 'term': 'TREMOR'},\n",
        " {'count': 3477, 'term': 'URINARY TRACT INFECTION'},\n",
        " {'count': 3433, 'term': 'SYNCOPE'},\n",
        " {'count': 3341, 'term': 'CORONARY ARTERY DISEASE'},\n",
        " {'count': 3316, 'term': 'HYPERSENSITIVITY'},\n",
        " {'count': 3170, 'term': 'DYSPEPSIA'},\n",
        " {'count': 3156, 'term': 'URTICARIA'},\n",
        " {'count': 3155, 'term': 'PALPITATIONS'},\n",
        " {'count': 3067, 'term': 'CONVULSION'},\n",
        " {'count': 3056, 'term': 'VISION BLURRED'},\n",
        " {'count': 2986, 'term': 'BLOOD CREATININE INCREASED'},\n",
        " {'count': 2955, 'term': 'MUSCULAR WEAKNESS'},\n",
        " {'count': 2943, 'term': 'INJECTION SITE PAIN'},\n",
        " {'count': 2939, 'term': 'GASTROOESOPHAGEAL REFLUX DISEASE'},\n",
        " {'count': 2909, 'term': 'DIABETES MELLITUS'},\n",
        " {'count': 2831, 'term': 'ABDOMINAL DISCOMFORT'},\n",
        " {'count': 2757, 'term': 'CHEST DISCOMFORT'},\n",
        " {'count': 2751, 'term': 'CARDIAC DISORDER'},\n",
        " {'count': 2702, 'term': 'CARDIAC ARREST'},\n",
        " {'count': 2683, 'term': 'HAEMORRHAGE'},\n",
        " {'count': 2665, 'term': 'CONTUSION'},\n",
        " {'count': 2611, 'term': 'CHILLS'},\n",
        " {'count': 2609, 'term': 'HEART RATE INCREASED'},\n",
        " {'count': 2585, 'term': 'TRANSIENT ISCHAEMIC ATTACK'},\n",
        " {'count': 2566, 'term': 'ALANINE AMINOTRANSFERASE INCREASED'},\n",
        " {'count': 2557, 'term': 'OVERDOSE'},\n",
        " {'count': 2505, 'term': 'THROMBOCYTOPENIA'},\n",
        " {'count': 2496, 'term': 'SEPSIS'},\n",
        " {'count': 2466, 'term': 'CARDIOVASCULAR DISORDER'},\n",
        " {'count': 2461, 'term': 'ARTHRITIS'},\n",
        " {'count': 1888, 'term': 'ASPARTATE AMINOTRANSFERASE INCREASED'},\n",
        " {'count': 1474, 'term': 'EMOTIONAL DISTRESS'},\n",
        " {'count': 1448, 'term': 'DYSPHAGIA'},\n",
        " {'count': 1448, 'term': 'BLOOD CHOLESTEROL INCREASED'},\n",
        " {'count': 1428, 'term': 'GENERAL PHYSICAL HEALTH DETERIORATION'},\n",
        " {'count': 1424, 'term': 'THROMBOSIS'}]"
       ]
      }
     ],
     "prompt_number": 89
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from operator import itemgetter\n",
      "map(itemgetter('count'), fda_list)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 87,
       "text": [
        "[15586,\n",
        " 13792,\n",
        " 12829,\n",
        " 12772,\n",
        " 12592,\n",
        " 12588,\n",
        " 11128,\n",
        " 11076,\n",
        " 10565,\n",
        " 10184,\n",
        " 10044,\n",
        " 9271,\n",
        " 8763,\n",
        " 8650,\n",
        " 8566,\n",
        " 8155,\n",
        " 7779,\n",
        " 7596,\n",
        " 7342,\n",
        " 7174,\n",
        " 7131,\n",
        " 6858,\n",
        " 6489,\n",
        " 6486,\n",
        " 6462,\n",
        " 6436,\n",
        " 6400,\n",
        " 6315,\n",
        " 5999,\n",
        " 5997,\n",
        " 5731,\n",
        " 5667,\n",
        " 5662,\n",
        " 5472,\n",
        " 5146,\n",
        " 5114,\n",
        " 4818,\n",
        " 4767,\n",
        " 4669,\n",
        " 4607,\n",
        " 4606,\n",
        " 4482,\n",
        " 4463,\n",
        " 4441,\n",
        " 4409,\n",
        " 4402,\n",
        " 4392,\n",
        " 4343,\n",
        " 4296,\n",
        " 4276,\n",
        " 4270,\n",
        " 4081,\n",
        " 3978,\n",
        " 3963,\n",
        " 3961,\n",
        " 3936,\n",
        " 3902,\n",
        " 3862,\n",
        " 3855,\n",
        " 3682,\n",
        " 3655,\n",
        " 3629,\n",
        " 3620,\n",
        " 3603,\n",
        " 3598,\n",
        " 3477,\n",
        " 3433,\n",
        " 3341,\n",
        " 3316,\n",
        " 3170,\n",
        " 3156,\n",
        " 3155,\n",
        " 3067,\n",
        " 3056,\n",
        " 2986,\n",
        " 2955,\n",
        " 2943,\n",
        " 2939,\n",
        " 2909,\n",
        " 2831,\n",
        " 2757,\n",
        " 2751,\n",
        " 2702,\n",
        " 2683,\n",
        " 2665,\n",
        " 2611,\n",
        " 2609,\n",
        " 2585,\n",
        " 2566,\n",
        " 2557,\n",
        " 2505,\n",
        " 2496,\n",
        " 2466,\n",
        " 2461,\n",
        " 1888,\n",
        " 1474,\n",
        " 1448,\n",
        " 1448,\n",
        " 1428,\n",
        " 1424]"
       ]
      }
     ],
     "prompt_number": 87
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}